package collection.list;

import lombok.Data;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
*@description: 对List的常用操作
*@author: kejl
*@create: 2020-07-04 12:40
*/
@Data
public class ListTest {

    /**
     * Collections.sort
     */
    @Test
    public void  testListSort(){
        Student t1 = new Student(3,"k");
        Student t2 = new Student(2,"l");
        Student t3 = new Student(1,"m");
        List<Student> studentList = new ArrayList<Student>();
        studentList.add(t1);
        studentList.add(t2);
        studentList.add(t3);
        Collections.sort(studentList, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if(o1.getAge()>o2.getAge()){
                    return 1;
                }else if(o1.getAge()==o2.getAge()){
                    return 0;
                }
                return -1;
            }
        });
        System.out.println(studentList);
    }

    @Data
    class Student{

        public Student(int age,String name){
            this.age = age;
            this.name = name;
        }

        private String name;
        private int age;
    }


}
